var app = getApp()
Page({
    data: {
        hideSheet: true,
        is_defaultCard: false,
        defaultCard_index: null,
        paying: false
    },
    onLoad(opition) {
        var res_data = JSON.parse(opition.res_data)
        var totalmey = opition.totalmey //净额
        var unionMail = opition.unionMail
        var newCount = opition.newCount // 购买总数
        var that = this
        that.setData({
            isChinese: wx.getStorageSync("isChinese"),
            totalmey: totalmey,
            unionMail: unionMail,
            res_data: res_data,
            newCount: newCount,
            discountNumber: opition.codes
        })

    },
    onShow() {
        let that = this
        wx.showLoading({
            title: '',
        })
        app.getApi.getProducts('POST', {
            method: "braintree.bTCustomer.get",
            id: app.globalData.unionId, //人ID
        }).then((res) => {
            if (res.data.customer.creditCards.length) {
                let creditCards = res.data.customer.creditCards
                Object.keys(creditCards).forEach((key) => {
                    if (creditCards[key].default) {
                        that.setData({
                            is_defaultCard: true,
                            defaultCard_index: key
                        })
                    }
                })
                that.setData({
                    creditCards: res.data.customer.creditCards
                })
            }
            wx.hideLoading()
        })
    },
    addCard() {
        let that = this
        wx.navigateTo({
            url: `../addcard/addcard?totalmey=${that.data.totalmey}&orderHeaderId=${that.data.res_data.id}`
        })
    },
    showActionSheet() {
        this.setData({
            hideSheet: false
        })
    },
    radioChange(e) {
        let that = this
        that.setData({
            defaultCard_index: e.detail.value,
            hideSheet: true
        })
    },
    closeWrap() { //点击蒙层
        this.setData({
            hideSheet: true
        })
    },
    goPay() { //支付

        let that = this
        let defaultCard_index = that.data.defaultCard_index
        let creditCards = that.data.creditCards

        if (that.data.paying) {

            return;

        }

        that.setData({ paying: true })
        wx.showLoading({
            title: '',
        })
        app.getApi.getProducts('POST', {
            amount: that.data.totalmey,
            method: "braintree.bTTransaction.create.new",
            payType: 'BankCard',
            discountNumber: that.data.discountNumber,
            orderHeaderId: that.data.res_data.id,
            paymentMethodToken: creditCards[defaultCard_index].token
        }).then((res) => {
            that.setData({ paying: false })
            wx.hideLoading()
            if (res.statusCode == 504) {
                wx.showModal({
                    title: that.data.isChinese ? '提示' : "Prompt",
                    showCancel: false,
                    content: that.data.isChinese ? "网络连接超时" : "Request timed out"
                })
                return;
            }
            if (res.data.isSuccess == 1) {
                app.globalData.newCount = that.data.newCount // 更新新购买票券数量
                wx.reLaunch({
                    url: '../paySucc/paySucc'
                })
            } else {
                wx.showModal({
                    title: that.data.isChinese ? "提示" : "Prompt",
                    showCancel: false,
                    content: res.data.firstErrorMessage
                })
                return;
            }
        }).catch(datas => {
            wx.hideLoading();
            that.setData({ paying: false })
        });
    }
})